(function ($) {
  'use strict';

  angular
    .module('articles')
    .filter('ArticlesTrim', ArticlesTrim)
    .controller('ArticlesController', ArticlesController);

  ArticlesController.$inject = ['$scope', 'articleResolve', 'Authentication', 'ArticlesService'];
  function ArticlesController($scope, article, Authentication, ArticlesService) {
    var vm = this;

    var newArt = new ArticlesService();
    newArt.copyExistArt(article);
    
    vm.article = newArt;
    vm.authentication = Authentication;

    vm.slide = function (dir) {
      // $('#listItemCarousel').carousel(dir);  //If using Carousel to switch img, need to set it
      if (dir === 'next') {
        vm.article.curImgIndex++;

        if(vm.article.curImgIndex >= vm.article.imgs.length) {
          vm.article.curImgIndex = 0;
        }
      }
      else if (dir === 'prev') {
        vm.article.curImgIndex--;

        if(vm.article.curImgIndex < 0) {
          vm.article.curImgIndex = vm.article.imgs.length - 1;
        }
      }
    };

    var myElement= document.getElementById('articleImgCarousel');
    var mc = new Hammer.Manager(myElement, {
      recognizers: [
        // RecognizerClass, [options], [recognizeWith, ...], [requireFailure, ...]
        // [Hammer.Rotate],
        // [Hammer.Pinch, { enable: false }, ['rotate']],
        [Hammer.Swipe,{ direction: Hammer.DIRECTION_HORIZONTAL }]
      ]
    });

    mc.on("swipeleft", function() {
      $scope.$apply(function () {
        vm.slide('next');
      });
    });

    mc.on("swiperight", function() {
      $scope.$apply(function () {
        vm.slide('prev');
      });
    });
  }

  ArticlesTrim.$inject = [];
  function ArticlesTrim() {
    return function (htmlContent) {
      var regex = new RegExp('<p><br/></p>', 'g'); // Make sure the /g not /gi, for we need case-sensitive.
      var trimLFCR = htmlContent.replace(regex, '');
      return trimLFCR;
    }
  }
}(jQuery));
